<template>
  <div>
    <!-- 搜索表单 -->
    <div style="text-align: center">
      <el-input style="width: 240px;" placeholder="请输入分类名称" v-model="params.name"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load"><i class="el-icon-search"
                                                                          style="margin-right: 5px"></i>搜索
      </el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh"
                                                                           style="margin-right: 5px"></i>重置
      </el-button>
    </div>
    <!-- 用户详细数据 -->
    <el-table :data="tableData" row-key="id" default-expand-all :cell-style="{'text-align':'center'}" :header-cell-style="{'text-align':'center'}">
      <el-table-column prop="id" label="编号"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="updatetime" label="更新时间"></el-table-column>
      <el-table-column label="操作" width="260px">
        <template v-slot="scope">
          <!-- scope.row就是当前行数据 -->
          <el-button type="success" @click="handleAdd(scope.row)" v-if="!scope.row.pid">添加二级分类</el-button>

          <el-button type="primary" @click="$router.push('/editCategory?id=' + scope.row.id )">编辑</el-button>

          <!-- 确认删除 -->
          <el-popconfirm
              title="您确定要删除该管理员账号吗？"
              style="margin-left: 10px"
              @confirm="del(scope.row.id)">
            <el-button slot="reference" type="danger">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗 -->
    <el-dialog title="添加二级分类" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="100px" ref="formRef" :rules="rules">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="分类备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="save">确 定</el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 分页 -->
    <div>
      <el-pagination style="margin-top: 10px; text-align: center" background :total="total" :page-size="params.pageSize"
                     :current-page="params.pageNum" layout="prev,pager,next" @current-change="handleCurrentChange">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";

export default {
  account: 'Category',
  data() {
    return {
      admin: Cookies.get('admin') ? JSON.parse(Cookies.get('admin')) : {},
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form: {
        pid: null,
      },
      params: {
        pageNum: 1,
        pageSize: 7,
        name: '',
      },
      rules: {
        name: [
          {required: true, message: '请输入分类名称', trigger: 'blur'},
        ],
      }
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request.get("/category/page", {
        params: this.params
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 7,
        name: '',
      }
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.load();
    },
    del(id) {
      request.delete("/category/" + id).then(res => {
        if (res.code === '200') {
          this.$notify.success('删除成功');
          this.load();
        } else {
          this.$notify.error(res.msg);
        }
      })
    },
    handleAdd(row) {
      //将当前行的id作为二级分类的id
      this.pid = row.id;
      this.dialogFormVisible = true;
    },
    save() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          //给二级分类赋值pid
          this.form.pid = this.pid;
          request.post('/category/', this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success('新增二级分类成功');
              this.$refs['formRef'].resetFields();
              this.dialogFormVisible = false;
              this.load();
            } else {
              this.$notify.error(res.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
  }
}
</script>

<style scoped>


</style>